package ar.com.equis.arquitectura.exception {
	import flash.events.EventDispatcher;
	
	import mx.controls.Alert;
	import mx.logging.ILogger;
	import mx.rpc.Fault;
	
	public class ExceptionHandler extends EventDispatcher {

		public static var instance:ExceptionHandler = new ExceptionHandler();
		
		public function handle(fault:Fault):void {
	    	trace(fault.toString());
			if(fault.rootCause is FlexException) {
				//La excepcion fue nuestra
				var own:FlexException = FlexException(fault.rootCause);
				this.handleOwn(own);
			} else {
				throw fault;
			}
		}
		
		public function handleOwn(own:FlexException):void {
			if(own.business){
				this.publishError(own.message);
			} else {
				var idString:String = own.id;
				Alert.show("Contact system administrator for further information or try again later. Please, write down the error report number: " + idString, "Application Error");
			}
		}
		
		public function publishError(errorName:String):void {
			Alert.show(errorName, "Error");
		}
	}
}